/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.tcp;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.zxy.adminlog.UsAdmin_Log;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

public class ServerThread extends Thread
{
	private ThreadPoolExecutor executorService2;
//	private ExecutorService	executorService;				// 线程
	private final int		INIT_SIZE	= 100;				// 单个CPU线程池
	private final int		POOL_SIZE	= 100;				// 单个CPU线程池
	private ServerSocket	accSocket;						// 接收数据socket
	//端口号
	private String			strInput	= "";
	//端口序号，第几个端口,从0开始
	private int			iNum		= 0;
	@SuppressWarnings(
	{ "rawtypes", "unused" })
	private ArrayList		clientList	= new ArrayList();

	public void setStrInput(String strInput)
	{
		this.strInput = strInput;
	}

	public int getINum()
	{
		return iNum;
	}

	public void setINum(int Num)
	{
		this.iNum = Num;
	}

	public ServerThread(String Input, int Num)
	{
		this.iNum = Num;
		this.strInput = Input;
	}

	public String getStrInput()
	{
		return strInput;
	}

	public void run()
	{
		try
		{
			this.setName("ServerThread_zxyong"+String.valueOf(this.iNum));
			accSocket = new ServerSocket(Integer.valueOf(this.strInput));
		}
		catch(IOException e)
		{
			Com_Fun.debug_in(e,this.toString(),String.valueOf(this.strInput)
					+ "端口已被占用");
		}
		// 创建一个定长线程池，可控制线程最大并发数，超出的线程会在队列中等待。
//		executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()
//				* POOL_SIZE);
		executorService2 =  (ThreadPoolExecutor) Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()
				* POOL_SIZE);
		executorService2.setCorePoolSize(Runtime.getRuntime().availableProcessors()
				* INIT_SIZE);
		executorService2.setKeepAliveTime(10, TimeUnit.SECONDS);
		executorService2.allowCoreThreadTimeOut(true);

		while(true)
		{
			Socket socket = null;
			try
			{
				socket = accSocket.accept();
				executorService2.execute(new ServerHandler(socket,this.iNum));
	            System.out.println("当前tcp线程池总数："+String.valueOf(executorService2.getPoolSize())+";其中有效线程数："+String.valueOf(executorService2.getActiveCount()));
				Thread.sleep(10);
				socket.sendUrgentData(0);
			}
			catch(Exception e)
			{
				Com_Para.htServSockets.remove("servSocket"
						+ socket.getInetAddress().getHostAddress()+"|"+String.valueOf(socket.getPort()));
				String TemChildNode = "";
				for(Enumeration<String> sm = Com_Para.htChildNode.keys();sm.hasMoreElements();)
				{
					TemChildNode = sm.nextElement();
					if(!java.util.Objects.equals(Com_Para.htChildNode.get(TemChildNode),null)
							&& Com_Para.htChildNode.get(TemChildNode).toString().equals("servSocket"
									+ socket.getInetAddress().getHostAddress()))
					{
						break;
					}
				}
				if(!TemChildNode.equals(""))
				{
					// Com_Para.htChildInfo.remove(TemChildNode);
					// Com_Para.htChildNode.remove(TemChildNode);
				}
			}
		}
	}
}